package com.lin.config;

import com.lin.task.Article2ESTask;
import com.lin.task.LikeTask;
import com.lin.task.LookTask;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @program: blog
 * @description: Quartz 定时器配置类
 * @author: Lin
 * @create: 2020-05-14 17:28
 **/

@Configuration
public class QuartzConfig {

    /**
     * 点赞定时任务
     */
    private static final String LIKE_TASK_IDENTITY = "LikeTaskQuartz";

    /**
     * 文章定时任务
     */
    private static final String Article_TASK_IDENTITY = "ArticleTaskQuartz";


    /**
     * 浏览定时任务
     */
    private static final String LOOK_TASK_IDENTITY = "LookTaskQuartz";


    /**
     * 点赞定时任务
     */
    @Bean
    public JobDetail likeTaskQuartzDetail(){
        return JobBuilder.newJob(LikeTask.class).withIdentity(LIKE_TASK_IDENTITY).storeDurably().build();
    }


    @Bean
    public Trigger likeTaskQuartzTrigger(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInMinutes(1) // 1分钟执行一次
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(likeTaskQuartzDetail())
                .withIdentity(LIKE_TASK_IDENTITY)
                .withSchedule(scheduleBuilder)
                .build();
    }

    /**
     * 浏览定时任务
     */
    @Bean
    public JobDetail lookTaskQuartzDetail(){
        return JobBuilder.newJob(LookTask.class).withIdentity(LOOK_TASK_IDENTITY).storeDurably().build();
    }


    @Bean
    public Trigger lookTaskQuartzTrigger(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInMinutes(1) // 1分钟执行一次
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(lookTaskQuartzDetail())
                .withIdentity(LOOK_TASK_IDENTITY)
                .withSchedule(scheduleBuilder)
                .build();
    }


    /**
     * 文章定时任务
     */
    @Bean
    public JobDetail articleTaskQuartzDetail(){
        return JobBuilder.newJob(Article2ESTask.class).withIdentity(Article_TASK_IDENTITY).storeDurably().build();
    }


    @Bean
    public Trigger articleTaskQuartzTrigger(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInMinutes(10)     // 10分钟执行一次
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(articleTaskQuartzDetail())
                .withIdentity(Article_TASK_IDENTITY)
                .withSchedule(scheduleBuilder)
                .build();
    }
}
